from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect,JsonResponse
from .models import MyModel
from django.contrib import messages
from django.forms import forms
from django.utils import timezone
time_now = timezone.now()

def index(request):
    #return HttpResponse("Blog")
    data = MyModel.objects.all()
    return render(request,"blog/index.html",{'data':data})

def add(request):
    data = MyModel(
        content = request.POST['a'],
        title = request.POST['title'],
        author = request.POST['author'],
        sort = request.POST['sort'],
        introduction = request.POST['introduction'],
        time = time_now.strftime("%y-%m-%d"),
    )
    data.save()
    res = {'status':'ok'}
    return JsonResponse(res)
    

def edit(request):
    return render(request, 'blog/edit.html')

def detail(request,article_id,article_author):
    if article_author.isdigit():
        data = MyModel.objects.get(id=article_id)
        count = 1
        return render(request,'blog/detail.html',{'data':data,'count':count})
    else:
        data = MyModel.objects.filter(author=article_author)
        count = 2
        return render(request,"blog/detail.html",{'data':data,'count':count})



